package com.nna88.voz.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nna88.voz.contain.containPage2;
import com.nna88.voz.main.R;
import com.nna88.voz.util.UserInfo;
public class listViewCustom2 extends BaseAdapter{
	public static final int GO_NEXT = 2;
	public static final int GO_PREVIOUS = 3;
	private ArrayList<containPage2> contains;
    private LayoutInflater inflater; 
    private ViewHolder holder;  
    private float mTextSize1;
    private float mTextSize2;
    private UserInfo mUser;
    private int state;
	public listViewCustom2(Activity context,ArrayList<containPage2> contain) {  
       // super();         
        mUser = new UserInfo();
        this.contains = contain;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
	}  
	public void setSize(float mTextSize){
	    TextView t1 = (TextView)inflater.inflate(R.layout.list_item, null).findViewById(R.id.text1);
		TextView t2 = (TextView)inflater.inflate(R.layout.list_item, null).findViewById(R.id.text2);
		this.mTextSize1 = t1.getTextSize()*mTextSize;
		this.mTextSize2 = t2.getTextSize()*mTextSize;

	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return contains.size(); 
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	public static class ViewHolder  
	{  
        ImageView imgView;  
        TextView txtView1;  
        TextView txtView2;
        ImageButton imagStar;
        RelativeLayout layout;
	}  
	private ColorStateList newColorStateList( int colorDefault, int colorPressed){
		return new ColorStateList(
    			new int[][]{new int [] {android.R.attr.state_pressed},new int [0]},
    			new int []{colorPressed,colorDefault});
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int pos;
		pos = position;
		final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector());
        if(convertView==null)  
        {  
            holder = new ViewHolder();  
            convertView = inflater.inflate(R.layout.list_item2, null);  
            holder.layout = (RelativeLayout) convertView.findViewById(R.id.listitem2);
            holder.imgView = (ImageView) convertView.findViewById(R.id.image);  
            holder.txtView1 = (TextView) convertView.findViewById(R.id.text1);  
            holder.txtView2 = (TextView) convertView.findViewById(R.id.text2);
            holder.imagStar = (ImageButton)convertView.findViewById(R.id.star);
            convertView.setTag(holder);  
        }  
        else  
            holder=(ViewHolder)convertView.getTag();  
        if((contains.get(position).Image()==null)&&(contains.get(position).UrlThread()==null)){
        	holder.txtView1.setTextColor(newColorStateList(Color.WHITE,Color.BLUE));
        	holder.layout.setBackgroundResource(R.color.vozTcat);
        	holder.txtView1.setText(contains.get(position).Thread()); 
        	holder.txtView2.setVisibility(View.GONE); 
        	holder.imagStar.setVisibility(View.GONE); 
        }else{
        	holder.txtView2.setVisibility(View.VISIBLE); 
            holder.layout.setBackgroundResource(R.color.vozThread);
            holder.txtView2.setTextColor(newColorStateList(Color.BLACK,Color.BLUE));
            if(contains.get(position).isSticky())
        		holder.txtView1.setTextColor(newColorStateList(Color.RED,Color.BLUE));
        	else
        		holder.txtView1.setTextColor(newColorStateList(Color.BLACK,Color.BLUE));
            if(mUser.isLogin() && 
            		!contains.get(position).UrlThread().contains("forumdisplay.php?f") &&
            		(contains.get(position).UrlLastPosst() != null))
            	holder.imagStar.setVisibility(View.VISIBLE);
            else
            	holder.imagStar.setVisibility(View.GONE);
            holder.txtView1.setText(contains.get(position).Thread());
            holder.txtView2.setText(contains.get(position).LastPost());  
           
        } 
             
        holder.txtView1.setTextSize(mTextSize1);
        holder.txtView2.setTextSize(mTextSize2);
        holder.imgView.setImageBitmap(contains.get(position).Image());
        
//        holder.txtView1.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				if(mItemClickListener != null){
//					mItemClickListener.onItemClick(pos, 1);
//				}
//			}
//		});
//        holder.imagStar.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View arg0) {
//				// TODO Auto-generated method stub
//				if(mItemClickListener != null){
//					mItemClickListener.onItemClick(pos, 2);
//				}
//			}
//		});
        holder.txtView1.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (gestureDetector.onTouchEvent(event)) {
					if(mItemClickListener != null){
						mItemClickListener.onItemClick(pos, 1,state);
					}
					
                }
				return false;
			}
		});
        holder.txtView2.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				if (gestureDetector.onTouchEvent(event)) {
					//Log.d("nna","touch true");
					if(mItemClickListener != null){
						mItemClickListener.onItemClick(pos, 2,state);
					}
					Log.d("nna","state,"+state);
					
                }else{
                	//Log.d("nna","touch false");
                }
                return false;
			}
		});
        holder.txtView1.setFocusable(true);
        holder.txtView1.setClickable(true);
        holder.txtView2.setFocusable(true);
        holder.txtView2.setClickable(true);
        return convertView;  
	}
	private OnActionItemClickListener mItemClickListener;
	public void setOnActionItemClickListener(OnActionItemClickListener listener) {
		mItemClickListener = listener;
	}
	public interface OnActionItemClickListener {
		public abstract void onItemClick( int pos, int actionId, int state);
	}
	class MyGestureDetector extends SimpleOnGestureListener{ 
    	private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;
        // Detect a single-click and call my own handler.
        @Override 
        public boolean onSingleTapUp(MotionEvent e) {
        	//Log.d("nna","single,");
        	state = 0;
            return true;
        }
        @Override 
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
        	boolean result = true;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                        	state = GO_PREVIOUS;
                            //Log.d("nna","Right,");
                        } else {
                        	state = GO_NEXT;
                           // Log.d("nna","Left,");
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        } 
    } 
	
	
}
